#!/usr/bin/perl   
use Encode;
use LWP;

my $board=seconddigi;
my $bid=835;
my $logfile="/home/bean/.get_information.log";
my $agent=LWP::UserAgent->new;
my $request =HTTP::Request->new(GET=>$urlToContent);
my $response=$agent->request($request);
$seconddigi=$response->content;
my $seconddigi=`wget http://www.newsmth.net/bbsdoc.php?board=$board -q -O -|cat`;
my $seconddigi=decode("gb2312",$seconddigi); 
my $seconddigi=encode("utf8",$seconddigi);
my @seconddigi_text=split /\n/,$seconddigi;

my @buffer;
my $buffer_size=100;
my $ptr=0;

my $success=open LOG,"<",$logfile;
if($success){
    foreach(<LOG>){
	chomp;
	push @buffer,$_;
	
    }
    $ptr=$#buffer+1;
}else{
    print STDERR "read fail";
}
close LOG;
foreach(@seconddigi_text){
    my $item=$_;
    my $aid,$paid,$author,$title,$attribution,$message,$content,$time;
    next unless ($item=~/^.*?\((\d+?),(\d+?),'(.*?)','(.*?)',\d+,'(.*?)'/);
    $aid=$1;
    $paid=$2;
    $author=$3;
    $title=$5;
    $attribution=$4;
    next if ($attribution eq "d ");
    next if ($title=~/^Re:/ || $title=~/\[合集\]/ || $title=~/权限/);
    my $urlToContent="http://www.newsmth.net/bbscon.php?bid=$bid&id=$aid";
    $request =HTTP::Request->new(GET=>$urlToContent);
    $response=$agent->request($request);
    $content=$response->content;

    $content=decode("gb2312",$content);
    $content=encode("utf8",$content);
    $content=~/^prints\(\'(.*)\'\)/m;
    $content=~/^prints\(\'(.*)\\n--\\n/m;
    $content=$1;
    $content=~s/发信人.*?\\n标  题.*?\\n发信站.*?(\d\d:\d\d:\d\d).*?\\n//g;
    $time=$1;
    $content=~s/\\n/\n/g;
    $content=~s/\n//mg;

    $message=$author." ".$title." ".$attribution;
  
    my $index;
    my $isnew=1;
    for($index=0;$index<$buffer_size;$index++){
	if($message eq $buffer[$index]){
	    $isnew=0;
	    last;
	}
    }
    if($isnew){
	print $message;
	print "@".$time."\n";
	print "内容:".$content."\n\n";
	$buffer[$ptr]=$message;
	$ptr++;
	$ptr=0 if $ptr==$buffer_size;
    }
}

$success=open LOG,">",$logfile;
if($success){
    foreach(@buffer){
	print LOG $_."\n";
    }
}else{
    print STDERR "write fail";
}

